package com.cjack.frenchexcel.controller;

import com.cjack.frenchexcel.config.AjaxResult;
import com.cjack.frenchexcel.exception.CommonException;
import com.cjack.frenchexcel.model.SystemparamModel;
import com.cjack.frenchexcel.service.SystemparamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

/**
 * 一些公用的key-value数据项管理
 * Created by root on 4/21/19.
 */
@RestController
@RequestMapping("/systemparam")
public class SystemparamEndpoint extends BaseEndpoint{

    @Autowired
    SystemparamService service;

    /**
     * 列表
     * @return
     */
    @RequestMapping(value = "/list/", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult list() {

        try{
            //注意pageDate里面的页数是从0开始计算的
            List<SystemparamModel> datas = service.list();
            return AjaxResult.SUCCESS( datas);
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 新增
     * @return
     */
    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult addOrUpdate( SystemparamModel model) {
        try{
            if( isAdd( model.getId())){
                model.setCreateTime( new Date());
            }
            service.add( model);
            return AjaxResult.SUCCESS();
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    /**
     * 获取
     * @return
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult get() {
        try{
            SystemparamModel model = service.getParam();
            return AjaxResult.SUCCESS( model);
        }catch ( Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
